package ecustom.common;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;

import ecustom.common.util.CustomUtil;

public class CustomMap extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;
	
	public CustomMap() {}
	
	public CustomMap(JSONObject jsonObj) {
		super.putAll(jsonObj);
	}
	
	public CustomMap(Map<String, Object> map) {
		super.putAll(map);
	}
	
	public Object getCK(String key) {
		return getCK(key, "Map “key=" + key + "” is blank!");
	}
	
	public Object getCK(String key, String msg) {
		Object value = get(key);
		if (value == null) throw new RuntimeException(msg);
		return value;
	}

	public boolean getBoolbean(String key) {
		Object value = get(key);
		if (value == null) return false;
		return value.toString().equalsIgnoreCase("TRUE");
	}
	
	public CustomMap getCustomMap(String key) {
		return (CustomMap)get(key);
	}

	public Double getDouble(String key) {
		return CustomUtil.getDouble(get(key));
	}

	public Double getDoubleCK(String key, String msg) {
		return CustomUtil.getDoubleCK(get(key), msg);
	}
	
	public Integer getInteger(String key) {
		return CustomUtil.getInteger(get(key));
	}
	
	public int getIntCK(String key) {
		return getIntCK(key, "The value of key is not a integer, key= + key + ");
	}
	
	public int getIntCK(String key, String msg) {
		return CustomUtil.getIntCK(get(key), msg);
	}
	
	public int getInt(String key) {
		return CustomUtil.getInt(get(key));
	}

	public List<?> getListCK(String key) {
		return (List<?>)get(key);
	}
	
	public List<?> getListCK(String key, String errMsg) {
		List<?> list = (List<?>)get(key);
		if (list == null || list.isEmpty()) throw new RuntimeException(errMsg);
		return list;
	}
	
	public Long getLongCK(String key) {
		return CustomUtil.getLong(get(key));
	}
	
	public String getString(String key) {
		return CustomUtil.getString(get(key));
	}
	
	public String getString(String key, String defValue) {
		return CustomUtil.getString(get(key), defValue);
	}
	
	public String getStringCK(String key) {
		return getStringCK(key, "Map “key=" + key + "” is blank!");
	}

	public String getStringCK(String key, String msg) {
		String value = CustomUtil.getString(get(key));
		if (CustomUtil.isBlank(value)) throw new RuntimeException(msg);
		return value;
	}
}